package org.wpahs.ahn.rssfeed;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;

import android.widget.ListView;

/**Wow Great
 * Created by dgambone on 01/21/2015.
 */
public class BounceListView extends ListView {

    private OnBounceEventListener mBounceUp;
    VelocityTracker mVelocityTracker;
    ViewConfiguration  mViewConfiguration;


    public BounceListView(Context context)
    {
        super(context);
        initBounceListView();
    }

    public BounceListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initBounceListView();
    }

    public BounceListView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        initBounceListView();
    }


    private void initBounceListView()
    {
        //get the density of the screen and do some maths with it on the max overscroll distance
        //variable so that you get similar behaviors no matter what the screen size
        setVerticalFadingEdgeEnabled(true);
        mViewConfiguration = ViewConfiguration.get(this.getContext());
        mVelocityTracker = VelocityTracker.obtain();
        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mVelocityTracker.clear();
                    mVelocityTracker.addMovement(event);
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    mVelocityTracker.computeCurrentVelocity(1000, mViewConfiguration.getScaledMaximumFlingVelocity());
                    //Log.i("vertical velocity", String.valueOf(mVelocityTracker.getYVelocity()));
                    BounceListView fl = (BounceListView) v;
                    /*
                    Log.i("Scroll", String.valueOf(mVelocityTracker.getYVelocity() + " - " +
                            -mViewConfiguration.getScaledMinimumFlingVelocity()) +
                            String.valueOf(fl.canScrollVertically(1)));
                    */
                    if (mVelocityTracker.getYVelocity() < -2900f &&
                            !fl.canScrollVertically(1)) {
                        // vertical fling!
                        mBounceUp.onFlungY();
                        mVelocityTracker.clear();
                        return true;
                    }
                }else if(event.getAction() == MotionEvent.ACTION_MOVE){
                    mVelocityTracker.addMovement(event);
                } else {
                    mVelocityTracker.clear();
                }
                return false;

            }
        });


    }

    public void setOnBounceUpListener(OnBounceEventListener el){
        mBounceUp = el;
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX,scrollY,clampedX,clampedY);
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
                                   int scrollY, int scrollRangeX, int scrollRangeY,
                                   int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        /*
        Log.i("overScrollBy", String.valueOf(deltaY) + " -- " +
                              String.valueOf(scrollRangeY) + " -- " +
                              String.valueOf(maxOverScrollY) + " -- " +
                              String.valueOf(isTouchEvent));
        if(isTouchEvent==true && deltaY > 50 && !alreadyHandledSwipeUp){
            mBounceUp.onScrollDownBounce(this);
        }
        */

        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY,
                scrollRangeX, scrollRangeY, maxOverScrollX,
                maxOverScrollY, isTouchEvent);

    }



    public interface OnBounceEventListener {
        public void onFlungY();
    }
}
